<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$_SESSION['forward_url']='jy_jd/hotel//id='.$_RQ['id'];
	cfc('core')->checklogin();
	if(!$_RQ['id']){
		exi('请先选择房型！','error','jy_jd/hotel/');
	}
	$room=pdo_get('jy_jd_room',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$room['status']){
		exi('该房间已下架！','error','jy_jd/hotel/');
	}
	$room['tkey']=$tkey=intval($_RQ['t']);
	$settime=$this->session('settime');
	$intime=$settime['intime']?:date("Y-m-d");
	$innum=$this->model('model')->check_innum($room,$intime);
	if($room['num']<=$innum){
		exi('该房型已无空房,请选择其他房型','error','jy_jd/hotel/');
	}
	$room['images']=json_decode($room['images'],true);
	$room['taocan']=json_decode($room['taocan'],true);
	$room['taocan']=$this->model('model')->get_uptaocan($room,$intime);
	$room['ttitle']=$room['taocan'][$tkey]['title'];
	$room['tprice']=$_SESSION['jd']['tprice']=$room['taocan'][$tkey]['price'];
	$hotel=pdo_get('jy_jd_hotel',array('id'=>$room['hotelid'],'uniacid'=>$_SESSION['uniacid']));
	$hotel['images']=json_decode($hotel['images'],true);
	if($hotel['images']){
		$return['imagenum']=count($hotel['images']);
	}
	$_SESSION['jd']['hotelid']=$hotel['id'];
	$_SESSION['jd']['roomid']=$room['id'];
	$_SESSION['jd']['tkey']=$tkey;
	//$return['nowprices']=$room['taocan'][$tkey]['nowprices'];
	$ontime=strtotime(date("Y-m-d"))-100;
	$nowdayprices=pdo_getall('jy_jd_room_price',array('taocankey'=>$tkey,'roomid'=>$room['id'],'ontime >='=>$ontime,'uniacid'=>$_SESSION['uniacid']),array('id','onday','price'),'onday');
	$return['nowdayprices']=$nowdayprices;
	$room['tprice']=$room['tprice']?:$room['price'];
	$return['room']=$room;
	$return['pcd']=$this->session('pcd')?:array('city'=>'');	
	$day=$settime['day']?:1;
	$res=$this->model('model')->get_upprice($room['id'],$intime,$tkey,1,$day);
	$yaprice=$res['yaprice'];
	$allprice=$res['allprice'];
	$return['post']=array(
		'num'=>'1',
		'intime'=>$intime,
		'endtime'=>$settime['endtime']?:date("Y-m-d",strtotime("+1 day")),
		'day'=>$day,
		'shike'=>'20:00',
		'allprice'=>$allprice,
		'yaprice'=>$yaprice
	);
	$return['post']['settime']=array(
			'tstart'=>strtotime($return['post']['intime']),
			'tend'=>strtotime($return['post']['endtime']),
	);
	$return['allprice']=$allprice;
	$return['hotel']=$hotel;
	$this->setting['diy_text01']=$hotel['diy_text01']?:$this->setting['diy_text01'];
	$this->setting['diy_text02']=$hotel['diy_text02']?:$this->setting['diy_text02'];
	$this->setting['diy_text03']=$hotel['diy_text03']?:$this->setting['diy_text03'];
	$diy_text01=$this->setting['diy_text01']?:'订单金额包含押金￥[iprice]/间';
	$diy_text02=$this->setting['diy_text02']?:'您预订的房间为内宾价格，须持大陆身份证才能入住！';
	$diy_text03=$this->setting['diy_text03']?:'取消规则：您可以在入住前[tui_day]天[tui_time]之前取消或者变更订单;在此之后取消或变更，将扣取您[tui_fee]元的退订手续费.';
	$diy_text01=str_replace('[iprice]',$room['iprice'],$diy_text01);
	$diy_text01=str_replace('[tui_time]',$room['tui_time'],$diy_text01);
	$diy_text01=str_replace('[tui_day]',$room['tui_day'],$diy_text01);
	$return['diy_text01']=str_replace('[tui_fee]',$room['tui_fee'],$diy_text01);
	$diy_text02=str_replace('[iprice]',$room['iprice'],$diy_text02);
	$diy_text02=str_replace('[tui_time]',$room['tui_time'],$diy_text02);
	$diy_text02=str_replace('[tui_day]',$room['tui_day'],$diy_text02);
	$return['diy_text02']=str_replace('[tui_fee]',$room['tui_fee'],$diy_text02);
	$diy_text03=str_replace('[iprice]',$room['iprice'],$diy_text03);
	$diy_text03=str_replace('[tui_time]',$room['tui_time'],$diy_text03);
	$diy_text03=str_replace('[tui_day]',$room['tui_day'],$diy_text03);
	$return['diy_text03']=str_replace('[tui_fee]',$room['tui_fee'],$diy_text03);
	$return['xieyi']=$this->plugin_setting('xieyi');
	exi($return);
	
}
if($_RQ['op']=='upprice'){
	$roomid=$_SESSION['jd']['roomid'];
	$hotelid=$_SESSION['jd']['hotelid'];
	$tkey=$_SESSION['jd']['tkey'];
	$post=get_postdata();
	$intime=$post['intime'];
	$num=$post['num'];
	$day=$post['day'];
	$res=$this->model('model')->get_upprice($roomid,$intime,$tkey,$num,$day);
	exi($res);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['check']){
		exi('请先阅读《订房须知》！','error');	
	}
	$roomid=$_SESSION['jd']['roomid'];
	$hotelid=$_SESSION['jd']['hotelid'];
	$tkey=$_SESSION['jd']['tkey'];
	$room=pdo_get('jy_jd_room',array('id'=>$roomid));
	if(!$room['status']){
		exi('该房间已下架！','error','jy_jd/hotel/');
	}
	$hotel=pdo_get('jy_jd_hotel',array('id'=>$hotelid));
	$post['intime']=substr($post['intime'],0,10);
	$res=$this->model('model')->get_upprice($roomid,$post['intime'],$tkey,$post['num'],$post['day']);
	$yafee=$res['yaprice'];
	$price=$res['allprice'];
	if(!$post['intime']){
		exi('请选择入住日期','error');	
	}
	if(!$post['day']){
		exi('请填写入住时长','error');	
	}
	if(!$post['num']){
		exi('请选择房间数量','error');	
	}
	if(!$post['shike']){
		exi('请选择保留时间','error');	
	}
	if(!$post['name']){
		exi('请填写入住人姓名','error');	
	}
	if(!$post['mobile']){
		exi('请填写联系号码','error');	
	}
	$innum=$this->model('model')->check_innum($room,$post['intime'],$post['day'],$post['num']);
	$name=$post['name'];
	if($post['name2']){
		$name .= ','.$post['name2'];
	}
	if($post['name3']){
		$name .= ','.$post['name3'];
	}
	if($post['name4']){
		$name .= ','.$post['name4'];
	}
	if($post['name5']){
		$name .= ','.$post['name5'];
	}
	if($post['num']==1){
		$allday=$post['intime'];
	}else{
		$allday=';'.$post['intime'].';';
		$time=strtotime($post['intime']);
		for($i=1;$i<$post['day'];$i++){
			$allday.=date("Y-m-d",$time+$i*24*3600).';';
		}
	}
	$room['taocan']=json_decode($room['taocan'],true);
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'hotelid'=>$hotelid,
		'roomid'=>$roomid,
		'name'=>$name,
		'mobile'=>$post['mobile'],
		'day'=>$post['day'],
		'num'=>$post['num'],
		'intime'=>$post['intime'],
		'allday'=>$allday,
		'order'=>get_ordersn('jy_jd_order','order'),
		'fee'=>$price,
		'yafee'=>$yafee,
		'mid'=>$_SESSION['uid'],
		'tkey'=>$tkey,
		'tkeytitle'=>$room['taocan'][$tkey]['title'],
		'createtime'=>TIMESTAMP		
	);
	if($hotel['userid']){
		$data['userid']=$hotel['userid'];
	}
	pdo_insert('jy_jd_order',$data);
	$params['id']		=  pdo_insertid();
	$params['fee']      =  $price;
	$params['title']   	=  '酒店预定';
	$params['plugin']	=  'jy_jd';
	//$params['no_type']=array('hdfk');//可选，此项作用为指定不允许使用哪种支付方式
	$params['back_url']='jy_jd/create/payresult/';	
	//print_r($params);exit();
	$pay_id=cfc('pay')->create_pay_order($params);
	$this->model('model')->upmember($hotelid,$name,$post['mobile']);
	pdo_update('jy_jd_order',array('pay_id'=>$pay_id),array('id'=>$params['id']));
	$return['orderid']=$params['id'];
	$return['payid']=$pay_id;
	exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	$order=pdo_get('jy_jd_order',array('id'=>$payresult['id']));
	if($order['fee']==$payresult['fee']){
		pdo_update('jy_jd_order',array('paytype'=>$payresult['pay_type'],'ispay'=>1,'status'=>0,'transid'=>$payresult['transaction_id']),array('id'=>$payresult['id']));
		$this->model('commission')->jy_commission($order);
		$this->model('notice')->notice_payorder($order);
		$this->model('notice')->notice_payorder2admin($order);
		exi('支付成功','success','jy_jd/order/');
	}else{
		exi('支付失败','error','jy_jd/order/');
	}
}